import dash
from dash import html, dcc
import feffery_antd_charts as fact
import feffery_antd_components as fac
import feffery_utils_components as fuc
import feffery_leaflet_components as flc
from dash.dependencies import Input, Output, State

from components import (
    rose_chart_demo,
    pie_chart_demo,
    map_demo,
    table_demo
)

app = dash.Dash(__name__, suppress_callback_exceptions=True)

app.layout = html.Div(
    [
        dcc.Store(
            id='layouts-restore',
            storage_type='session'
        ),
        html.Div(
            id='grid-demo-container'
        )
    ],
    style={
        'padding': '50px'
    }
)


@app.callback(
    Output('layouts-restore', 'data'),
    Input('grid-demo', 'layouts'),
    prevent_initial_call=True
)
def dump_layouts(layouts):

    if layouts:
        return layouts


@app.callback(
    Output('grid-demo-container', 'children'),
    Input('layouts-restore', 'data')
)
def render(data):

    return fuc.FefferyGrid(
        [
            fuc.FefferyGridItem(
                [
                    rose_chart_demo
                ],
                key='grid1'
            ),
            fuc.FefferyGridItem(
                [
                    pie_chart_demo
                ],
                key='grid2'
            ),
            fuc.FefferyGridItem(
                [
                    map_demo
                ],
                key='grid3'
            ),
            fuc.FefferyGridItem(
                [
                    table_demo
                ],
                key='grid4'
            )
        ],
        layouts=data or [
            dict(
                i='grid1',
                x=0,
                y=0,
                w=3,
                h=2
            ),
            dict(
                i='grid2',
                x=3,
                y=0,
                w=3,
                h=2
            ),
            dict(
                i='grid3',
                x=1,
                y=2,
                w=3,
                h=3
            ),
            dict(
                i='grid4',
                x=3,
                y=3,
                w=2,
                h=4
            )
        ],
        id='grid-demo',
        cols=12,
        rowHeight=100,
        placeholderBorderRadius='5px',
        margin=[15, 15],
        isBounded=False,
        autoSize=False
    )


if __name__ == '__main__':
    app.run(debug=True)
